<?php
/**
 * DYSHOP
 * ============================================================================
 * 版权所有 2005-2028 杭州鼎易信息科技有限公司，并保留所有权利。
 * 网站地址: https://www.doing.net.cn
 * ==========================================
 * 本系统由鼎易科技团队开发 156791994@qq.com
 * ============================================================================
 * $Author: TANGYIN 2020-06-06 $
 *
 */

namespace app\admin\controller;
use think\facade\View;

use app\common\logic\ModuleLogic;
use think\facade\Db;
use think\facade\Cache;

use app\common\model\SystemMenu as SystemMenuModel;
use app\common\model\Category as CategoryModel;

use app\common\model\Site as CommonMode;
use app\admin\validate\Site as validateCommon;

class Site extends Base
{
    function list(){
        if(IS_AJAX){
            $model = new CommonMode();
            $params = input('param.');
            $res = $model->tableData($params);
            return dyajaxReturn(1,'获取成功',$res['data'],'',$res['count']);
        }else {
            $param = input('param.');
            $cate_id = isset($param['cate_id'])?$param['cate_id']:'';
            $module = 'info';

            //获取栏目列表
            $CategoryModel = new CategoryModel();
            $treeList = $CategoryModel->treeList([]);
            View::assign('treeList',$treeList);

            View::assign('cate_id',$cate_id);
            View::assign('module',$module);
            return View::fetch();
        }
    }

    public function add(){
        $param = input('param.');
        $model = new CommonMode();
        if(IS_POST){
            $validate = new validateCommon;
            $result = $validate->scene('add')->batch(false)->check($param);
            if(!$result){
                return dyajaxReturn(0,$validate->getError());
            }
            $res = $model -> handle($param);
            return dyajaxReturn($res['code'],$res['msg'],$param);
        }else {

            View::assign('title',"站点");
            View::assign('add_time',date('Y-m-d'));

            $sort = $model->max("orderid");
            View::assign('sort',$sort+1);

            return View::fetch();
        }
    }


    public function edit(){
        $param = input('param.');
        $model = new CommonMode();
        if(IS_POST){
            $param = input('param.');

            $validate = new validateCommon;
            $result = $validate->scene('edit')->batch(false)->check($param);
            if(!$result){
                return dyajaxReturn(0,$validate->getError());
            }
            $res = $model -> handle($param);
            return dyajaxReturn($res['code'],$res['msg'],$param);
        }else{
            $site_id = $param['site_id'];
            $info = $model->where('site_id','=',$site_id)->find();

            View::assign('site_id',$site_id);
            View::assign('info',$info);
            View::assign('title','站点');
            return View::fetch();
        }
    }

    //删除
    public function delete(){
        $param = input('param.');

        $model = new CommonMode();
        $res = $model -> del($param);

        return dyajaxReturn($res['code'],$res['msg']);
    }


    //状态改变
    public function changeStatus(){
        $param = input('param.');
        $table = $param['table'];
        $id_name = $param['id_name'];
        $id = $param['id'];
        $field = $param['field'];
        $checked = $param['checked'];

        if($checked == 'true'){
            $res = DB::name($table) -> where($id_name,'=',$id)->update([$field=>1]);
        }else{
            $res = DB::name($table) -> where($id_name,'=',$id)->update([$field=>2]);
        }
        if(!$res){
            return dyajaxReturn(0,'操作失败');
        }
        return dyajaxReturn(1,'操作成功');
    }

}